/*--------------------------------------------------------------------------------------------------
Yatak Operation System Project Copyright 2012-2013
Author : Ilya Churaev
Started : 29 Jan 2013		Updated : 6 Mar 2013
File Name : login.c			Purpose : Login user
--------------------------------------------------------------------------------------------------*/
#include <yatak/paging.h>
#include <yatak/debug.h>
#include <yatak/yatak.h>
#include <yatak/kheap.h>
#include <yatak/mutex.h>
#include <screen.h>
#include <types.h>
#include <shell.h>
#include <users.h>
#include <sched.h>
#include <fs.h>

static char username [MAX_USERS] [256] = {'\0'};
uid_t uid_u = 0;

// I need to create a list for previos users... In order to I can get switch them.

//running task
extern volatile task_t *current_task;

//linked list
extern volatile task_t *ready_queue;

extern page_directory_t *current_directory;
extern uint32 read_eip();

inline uid_t GetUserId ()
{
	return uid_u;
}

char * FindUserName (uid_t uid)
{
	return username [uid];
}

char * whoami ()
{
	return username[GetUserId()];
}

void init_user ()
{
	char str [256];
	//uint32 flags;
	static mutex_t mutex;
	
	sprintf (str,"init%s.yk",whoami ());
	fs_node_t *file = finddir_fs(GetCurrentNodeDir(), str);
	if (file)
	{
		//save_flags (flags);
		//cli();
		
		mutex_enter (&mutex);
		
		shell_interp (file);
		
		mutex_leave (&mutex);
	
		//restore_flags (flags);
	}
}

static void AddUsers ()
{
	static int count = 0;
	char * dbuff;
	char uname [256];
	int uid = -1;
	int seek = 0;
	if (!count)
	{
		++count;
		fs_node_t *file = finddir_fs(GetCurrentNodeDir(), "user.conf");
		if (!file)
		{
			dprintf ("[login] Cannot open file: user.conf\n");
			uid_u = 0;
			strcpy(username [uid_u],"root");
			return -4;
		}
		do
		{	
			dbuff = rsa_decoder(file,&seek);
			if (!dbuff)
				break;
			char * param = (char *) kmalloc(sizeof(char)*256);
			char * sparam = param;
			strcpy (param, dbuff);
			char * line = strsep (&param," ");
			if (line)
			{
				strcpy (uname, line);
			}
			else
			{
				dprintf ("[login] Unknown format of file: user.conf\n");
				uid_u = 0;
				strcpy(username [uid_u],"root");
				kfree (dbuff);
				kfree (sparam);
				return -1;
			}
			line = strsep (&param," ");
			if (line)
			{
				uid = atoi (line);
			}
			else
			{
				dprintf ("[login] Unknown format of file: user.conf\n");
				uid_u = 0;
				strcpy(username [uid_u],"root");
				kfree (dbuff);
				kfree (sparam);
				return -1;
			}
			strcpy(username [uid],uname);
			kfree (dbuff);
			kfree (sparam);
		}
		while (1);
	}
}

int login (char * user, char * passwd)
{
	char * dbuff;
	char uname [256];
	int uid = -1;
	int seek = 0;
	fs_node_t *file = finddir_fs(GetCurrentNodeDir(), "user.conf");
	
	AddUsers ();
	
	if (!file)
	{
		dprintf ("[login] Cannot open file: user.conf\n");
		return -4;
	}
	do
	{	
		dbuff = rsa_decoder(file,&seek);
		if (!dbuff)
			break;
		char * param = (char *) kmalloc(sizeof(char)*256);
		char * sparam = param;
		strcpy (param, dbuff);
		char * line = strsep (&param," ");
		if (line)
		{
			if (strcmp (user, line))
			{
				kfree (dbuff);
				kfree (sparam);
				continue;
			}
			strcpy (uname, line);
		}
		else
		{
			dprintf ("[login] Unknown format of file: user.conf\n");
			return -1;
		}
		line = strsep (&param," ");
		if (line)
		{
			uid = atoi (line);
		}
		else
		{
			dprintf ("[login] Unknown format of file: user.conf\n");
			kfree (dbuff);
			kfree (sparam);
			return -1;
		}
		if (param)
		{
			if (strcmp (passwd, param))
			{
				kfree (dbuff);
				kfree (sparam);
				return -2;
			}
			uid_u = uid;
			kfree (sparam);
			return register_user (uname, uid);
		}
		else
		{
			uid_u = uid;
			kfree (dbuff);
			kfree (sparam);
			// User hasn't password
			return register_user (uname, uid);
		}
		kfree (dbuff);
		kfree (sparam);
	}
	while (1);
	dprintf ("[login] User cannot be found...\n");
	return uid;
}
